package com.hk.web.core.realm;

import com.hk.dao.manage.user.enums.UserStatusEnum;
import com.hk.dao.manage.user.mapper.ManagerUserMapper;
import com.hk.dao.manage.user.vo.ManagerUserLoginVO;
import com.hk.manage.login.enums.UserErrorEnum;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class ShiroRealm extends AuthorizingRealm {

    @Autowired
    public ManagerUserMapper mapper;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenToken;
        String username = token.getUsername();
        ManagerUserLoginVO user = mapper.checkLogin(username);
        if (user == null ) {
            throw new AccountException(UserErrorEnum.LOGIN_ERROR_ENUM.getCode());
        }
        if (user.getStatus().equals(UserStatusEnum.DISABLE)){
            throw new AccountException(UserErrorEnum.LOGIN_ERROR_ENUM.getCode());
        }
        return new SimpleAuthenticationInfo(user, user.getPassword(), user.getUsername());
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        ManagerUserLoginVO managerUserDO = (ManagerUserLoginVO) principalCollection.getPrimaryPrincipal();
//        if (managerUserDO == null){
//                throw new AuthenticationException(SysCode.USER_NOT_LOGIN.getCode());
//        }
//        List<SysPermission> permissions = managerUserDO.getPermissions();
//        if (CollectionUtils.isEmpty(permissions)){
//            return null;
//        }
//        Set<String> perms = new HashSet<>();
//        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//        authorizationInfo.setStringPermissions();
        return null;
    }


}
